/**
 * 热量表历史记录
 */
'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const decimal = require("decimal");
const {timeComponent,chartDataProcessing} = require("../../libs/Utils")
const columnNameObj = require("./deviceChartConfig").chart.heat;
const deviceType = require("./deviceChartConfig").type.heat;

module.exports = async function (params) {
    try{
    let timeInterval = timeComponent(params.startTime,params.endTime,params.type);
    let sql = `SELECT
        dd.device_no AS deviceNo,
        DATE_FORMAT(dd.data_time,"%Y-%m-%d %H:%i:%s") AS dataTime,
        ${timeInterval.queryGroupTime} 
        df.device_name AS deviceName,
        dd.instantaneous_heat AS instantaneousHeat,
        dd.accumulated_heat AS accumulatedHeat,
        dd.consumption_heat AS consumptionHeat,
        dd.instantaneous_flow AS instantaneousFlow,
        dd.accumulated_flow AS accumulatedFlow,   
        dd.consumption_flow AS consumptionFlow,   
        dd.supply_water_temperature AS supplyWaterTemperature,
        dd.return_water_temperature AS returnWaterTemperature,
        rf.range_name AS rangeName
    FROM
    data_heat_${params.userDomainId} dd
    INNER JOIN device_file df ON df.device_no = dd.device_no
    AND df.delete_sign = 1 AND df.device_type = ${deviceType} 
    AND df.range_id IN (?)
    INNER JOIN range_file rf ON df.range_id = rf.range_id AND rf.delete_sign = 1 
    WHERE 1 = 1 ${timeInterval.startTime && timeInterval.endTime ? ` AND (dd.data_time > "${timeInterval.startTime}" AND dd.data_time <= "${timeInterval.endTime}")` : `` } 
    ORDER BY dd.data_time DESC`;
    let heatDataList = await conn(sql,[params.rangeIds]);
    let heatDataGroup = _.groupBy(heatDataList,"deviceNo");
    let dataList = [];
    for(const deviceNo in heatDataGroup){
        let deviceDataList = heatDataGroup[deviceNo];
        let deviceObj = {
            deviceName:deviceDataList[0].deviceName,
            rangeName:deviceDataList[0].rangeName,
            deviceNo:deviceNo,
            instantaneousHeat:deviceDataList[0].instantaneousHeat,
            accumulatedHeat:deviceDataList[0].instantaneousHeat,
            consumptionHeat:0,
            instantaneousFlow:deviceDataList[0].instantaneousFlow,
            accumulatedFlow:deviceDataList[0].accumulatedFlow,
            consumptionFlow:0,
            supplyWaterTemperature:deviceDataList[0].supplyWaterTemperature,
            returnWaterTemperature:deviceDataList[0].returnWaterTemperature,
            dataList:[],
        }
        deviceDataList.forEach(function (data){
            deviceObj.consumptionHeat =  decimal.add(deviceObj.consumptionHeat,data.consumptionHeat).internal
            deviceObj.consumptionFlow =  decimal.add(deviceObj.consumptionFlow,data.consumptionFlow).internal
        })
        if(params.type === "hour"){
            deviceObj.dataList = deviceDataList
        }else {
            let timeGroup = _.groupBy(deviceDataList,"groupTime");
            for(const time in timeGroup){
                let timeGroupList = timeGroup[time];
                let deviceDataObj = {
                    dataTime:time,
                    dataCreateTime:timeGroupList[timeGroupList.length - 1].dataTime,
                    instantaneousHeat:timeGroupList[0].instantaneousHeat,
                    instantaneousFlow:timeGroupList[0].instantaneousFlow,
                    supplyWaterTemperature:timeGroupList[0].supplyWaterTemperature,
                    returnWaterTemperature:timeGroupList[0].returnWaterTemperature,
                    accumulatedHeat:timeGroupList[0].instantaneousHeat,
                    accumulatedFlow:timeGroupList[0].accumulatedFlow,
                    consumptionHeat:0,
                    consumptionFlow:0,
                }
                deviceDataList.forEach(function (data){
                    deviceDataObj.consumptionHeat =  decimal.add(deviceDataObj.consumptionHeat,data.consumptionHeat).internal
                    deviceDataObj.consumptionFlow =  decimal.add(deviceDataObj.consumptionFlow,data.consumptionFlow).internal
                })
                deviceObj.dataList.push(deviceDataObj);
            }
        }
        deviceObj.chart = chartDataProcessing(deviceObj.dataList,columnNameObj);
        dataList.push(deviceObj);
    }
    return { success: true,dataList:dataList};
    }catch (e){
        return { success: false,dataList:[],message:e.code === "ER_NO_SUCH_TABLE" ? "当前账号无此类设备" : e.sqlMessage};
    }
};